<template>
  <div>
    <introduce>
      <span class="title-gray" slot="pre">自定义类型/</span>
      <span class="title-bold" slot="title"> 测量类型</span>
      <span slot="info">
        -
        展示所有自定义的测量类型，提供了添加测量类型、修改测量类型、删除测量类型等功能。</span
      >
    </introduce>
    <el-table :data="measureList" stripe style="width: 100%; margin-top: 10px">
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <el-table-column prop="id" label="测量ID" width="180"> </el-table-column>
      <el-table-column prop="name" label="测量名称" width="180">
      </el-table-column>
      <el-table-column prop="unit" label="测量单位"> </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.cTime | dateformat }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间">
        <template slot-scope="scope">
          {{ scope.row.uTime | dateformat }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="
              dialogVisible = true;
              ruleForm = scope.row;
            "
            type="primary"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            @click="remove(scope.row)"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改测量类型" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="测量名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="测量单位" prop="unit">
          <el-input v-model="ruleForm.unit"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Introduce from "@/components/Introduce.vue";
import { $measureList, $measureDel, $measureEdit } from "@/api/index";
export default {
  name: "measurementList",
  components: { Introduce },
  data() {
    return {
      measureList: [],
      dialogVisible: false,
      ruleForm: {
        name: "",
        unit: "",
        id: "",
      },
      rules: {},
    };
  },
  methods: {
    getMeasureList() {
      $measureList().then((res) => {
        this.measureList = res.data.data;
      });
    },
    edit() {
      $measureEdit(this.ruleForm).then((res) => {
        if (res.data.msg == "success") {
          this.dialogVisible = false;
        }
      });
      this.getMeasureList();
    },
    remove(row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          $measureDel({ id: row.id }).then((res) => {
            console.log(res);
            this.$message.success("删除成功");
          });
          this.getMeasureList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.getMeasureList();
  },
  filters: {
    dateformat(val) {
      let t = new Date(+val);
      let y = t.getFullYear();
      let m = (t.getMonth() + 1 + "").padStart(2, "0");
      let d = (t.getDate() + "").padStart(2, "0");
      return `${y}-${m}-${d}`;
    },
  },
};
</script>

<style>
</style>